﻿ 
    using System;
    using System.Management;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Text;
namespace Time.OPCServerV
{
    public class ComputerInfo
    {
        private static char[] Charcode = new char[0x19];
        private static int[] intCode = new int[0x7f];
        private static int[] intNumber = new int[0x19];
        private ManagementScope ms;
        private ObjectQuery query;

        public string GetBaseBoardInfo()
        {
            string str = "";
            this.query = new ObjectQuery("select * from Win32_BaseBoard");
            ManagementObjectCollection objects = new ManagementObjectSearcher(this.ms, this.query).Get();
            foreach (ManagementObject obj2 in objects)
            {
                str = obj2["SerialNumber"].ToString();
            }
            return str;
        }

        public string GetCpuInfo_Remote()
        {
            string str = "";
            this.query = new ObjectQuery("select * from Win32_Processor");
            ManagementObjectCollection objects = new ManagementObjectSearcher(this.ms, this.query).Get();
            foreach (ManagementObject obj2 in objects)
            {
                str = obj2["ProcessorId"].ToString();
            }
            return str;
        }

        public string GetHardDiskInfo_Remote()
        {
            string str = "";
            this.query = new ObjectQuery("select * from Win32_PhysicalMedia");
            ManagementObjectCollection objects = new ManagementObjectSearcher(this.ms, this.query).Get();
            foreach (ManagementObject obj2 in objects)
            {
                str = obj2["SerialNumber"].ToString();
            }
            return str;
        }

        public void GetHardInformation_Remote(string ip)
        {
            ConnectionOptions options = new ConnectionOptions();
            this.ms = new ManagementScope(@"\\" + ip, options);
            this.ms.Connect();
        }

        public void GetHardInformation_Remote(string ip, string uname, string upwd)
        {
            ConnectionOptions options = new ConnectionOptions {
                Username = uname,
                Password = upwd
            };
            this.ms = new ManagementScope(@"\\" + ip, options);
            this.ms.Connect();
        }

        public static string GetLocalCPUSN()
        {
            ManagementObjectCollection instances = new ManagementClass("win32_Processor").GetInstances();
            foreach (ManagementObject obj2 in instances)
            {
                return obj2.Properties["Processorid"].Value.ToString();
            }
            return null;
        }

        public static string GetLocalDVSN()
        {
            ManagementClass class2 = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject obj2 = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            obj2.Get();
            return obj2.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        public static string[] GetLocalIP()
        {
            IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            string[] strArray = new string[hostAddresses.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                strArray[i] = hostAddresses[i].ToString();
            }
            return strArray;
        }

        public static string GetLocalMac()
        {
            string str = string.Empty;
            ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
            foreach (ManagementObject obj2 in instances)
            {
                if ((bool) obj2["IPEnabled"])
                {
                    str = str + obj2["MacAddress"].ToString();
                }
            }
            return str.ToUpper();
        }

        public static string getMacAddr_Remote(string RemoteIP)
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                int destIP = inet_addr(RemoteIP);
                long macAddr = 0L;
                int phyAddrLen = 6;
                SendARP(destIP, 0, ref macAddr, ref phyAddrLen);
                string str = Convert.ToString(macAddr, 0x10).PadLeft(12, '0').ToUpper();
                int num4 = 12;
                for (int i = 0; i < 6; i++)
                {
                    if (i == 5)
                    {
                        builder.Append(str.Substring(num4 - 2, 2));
                    }
                    else
                    {
                        builder.Append(str.Substring(num4 - 2, 2) + "-");
                    }
                    num4 -= 2;
                }
                return builder.ToString();
            }
            catch
            {
                return builder.ToString();
            }
        }

        public string GetNetWorkInfo_Remote()
        {
            string str = "";
            this.query = new ObjectQuery("select * from Win32_NetworkAdapter");
            ManagementObjectCollection objects = new ManagementObjectSearcher(this.ms, this.query).Get();
            foreach (ManagementObject obj2 in objects)
            {
                str = obj2["MACAddress"].ToString();
            }
            return str;
        }

        [DllImport("Ws2_32.dll")]
        private static extern int inet_addr(string ipaddr);
        public static string[] M()
        {
            string str = string.Empty;
            ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
            foreach (ManagementObject obj2 in instances)
            {
                if ((obj2["MacAddress"] != null) && !str.Contains(obj2["MacAddress"].ToString()))
                {
                    str = str + obj2["MacAddress"].ToString() + "|";
                }
            }
            if (str.Length > 0)
            {
                str = str.Substring(0, str.Length - 1);
            }
            return str.ToUpper().Split(new char[] { '|' });
        }

        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(int DestIP, int SrcIP, ref long MacAddr, ref int PhyAddrLen);
    }
}

